<?php

namespace Admin\Controller;

class TecholderController  extends AdminController {
	public function tecach(){
	    $tecach	=	M('TbTecach');
        $tecname = I('tecname', '', 'text');
        
//         $str=$tecach->getField('tID');
//         $arr=explode(',', $str); 
        
//         $map['status'] = array('egt', 0);
//         $map['cID'] = UID;
//         if ($tecname !== '') {
//             $map['tecname'] = array('like', '%' . (string)$tecname . '%');
//         }
//         $map['duijie_tb_lable.id']=array('in',$str);
//         dump($map);
        $list=$tecach->join('duijie_tb_teccoo on duijie_tb_tecach.hztype=duijie_tb_teccoo.cid')
        ->join('duijie_tb_teccsd on duijie_tb_tecach.csd=duijie_tb_teccsd.sid')
        ->field('duijie_tb_tecach.*,duijie_tb_teccsd.csd,duijie_tb_teccoo.cootype')
        ->select();
        foreach ($list as $key => $value) {
        	$str=$list[$key]['tID'];
        	$map['id']=array('in',$str);
        	$list[$key]['tids']=M("TbLable")->where($map)->select();
        }
        foreach ($list as $key => $value) {
        	$str1=$list[$key]['labfid'];
        	$str2=$list[$key]['labkid'];
        	$map1['ID']=$str1;
        	$map2['ID']=$str2;
        	$list[$key]['tids1']=M("TbTeclab")->where($map1)->select();
        	$list[$key]['tids2']=M("TbTeclab")->where($map2)->select();
        }
        //dump($list);
       //$list = $this->lists($tecach, $map);
       //$arr=explode(',', $list['tID']);
        //int_to_string($list,array('djstatus'=>array(0=>'未对接',1=>'对接中',2=>'已对接')));              //数字转为字符串
        
        $this->assign('_list', $list);
     $this->display();
      }
	
	
      public function add(){
      	if(IS_POST){
      		$teach = D("TbTecach");
      		$data = $teach->create();
      		$data['cID'] = UID;
      		$rst = $teach->add($data);
      		
      		$tid = implode(',', $rst['tID']);
      		$rst['tID'] = $tid;
      		
      		$patentid = implode(',',$rst['patentid']);
      		$rst['patentid']=$patentid;
      		
      		$awardid = implode(',',$rst['awardid']);
      		$rst['awardid']=$awardid;
      		
      		$paperid = implode(',',$rst['paperid']);
      		$rst['paperid']=$paperid;
      		
      		if($rst){
      			$this->success("添加成功",U("tecach"));
      		}else{
      			$this->error("添加失败");
      		}
      	} 
	
       	$result = M('tb_teclab')->where(array('upid'=> 0))->field('id,name')->select();
       	$this->assign('cate',$result);
      	
       	$result = M('tb_teclab')->where(array('upid'=> 0))->field('id,name')->select();
       	$this->assign('cate',$result);
       	
       	$pat= M('TbPatent')->where('duijie_tb_patent.cID'==UID)->field('id,patent')->select();
       	$this->assign('pat',$pat);
       	
       	$aw= M('TbAward')->where('duijie_tb_award.cID'==UID)->field('id,aname')->select();
       	$this->assign('aw',$aw);
       	
       	$per= M('TbPaper')->where('duijie_tb_paper.cID'==UID)->field('id,pname')->select();
       	$this->assign('per',$per);
       	
       /* 	dump($pat);
       	dump($aw);
       	dump($per); */
       	
       	$this->display('edit');
      	
      }
      
      public function edit($id=0){
      	if(IS_POST){
      		$tecach	=	M('TbTecach');      		
      		$rst=$tecach->create();
      		$data = implode(',', $rst['tID']);
      		$rst['tID'] = $data;
      		
      		$patentid = implode(',',$rst['patentid']);
      		$rst['patentid']=$patentid;
      		
      		$awardid = implode(',',$rst['awardid']);
      		$rst['awardid']=$awardid;
      		
      		$paperid = implode(',',$rst['paperid']);
      		$rst['paperid']=$paperid;
      	    //dump($rst);
      		//dump($rst);
      		if ($rst){
      			if($tecach->save($rst)!==false){
      				$this->success(L('_SUCCESS_UPDATE_'),U('tecach'));
      			    }else {
      				    $this->error(L('_FAIL_UPDATE_'));
      			}
      		}else {
      				$this->error($tecach->getError());
      			}
      	}else {
      		$info = array();
      		
      		$tecach	=	M('TbTecach');
      		$data=$tecach->find($id);
      		$this->assign('data', $data);
      		
      		//dump($data);
      		
      		$result = M('tb_teclab')->where(array('upid'=> 0))->field('id,name')->select();
      		$this->assign('cate',$result);
      		
      		$pat= M('TbPatent')->where('duijie_tb_patent.cID'==UID)->field('id,patent')->select();
      		$this->assign('pat',$pat);
      		
      		$aw= M('TbAward')->where('duijie_tb_award.cID'==UID)->field('id,aname')->select();
      		$this->assign('aw',$aw);

      		$per= M('TbPaper')->where('duijie_tb_paper.cID'==UID)->field('id,pname')->select();
      		$this->assign('per',$per);
      		              		
      		 $this->display();
      	}
      		/* $id=I('id');
      		
      		$tecach	=	M('TbTecach');
      		$data=$tecach->find($id);
      		$this->assign('data', $data);
      		
      		$result = M('tb_teclab')->where(array('upid'=> 0))->field('id,name')->select();
      		$this->assign('cate',$result);

      		$lable=M('TbLable')->select();
      		$this->assign('n_list',$lable);
      		
      		$rst=$tecach->join("duijie_tb_lable on duijie_tb_tecach.tID = duijie_tb_lable.id")
      		->where('duijie_tb_tecach.id'==$id)->select();

      		$this->display('edit'); */
  	
      }
      
      public function lable(){
      	$result = array();
      	$cate =$_POST['type'];
      	$result = M('tb_teclab')->where(array('upid'=> $cate))->field('id,name')->select();
      	$this->ajaxReturn($result,"JSON");
      }
      
      public function save(){
      	$tecach	=	M('TbTecach');
      	$ach	=	$tecach->create();
      	if(empty($ach['id'])){
      		$ach['cID']=UID;
      		$tecach->add($ach);
      		$this->success("技术成果添加成功！",U('tecach'));
      	}else {
      		$tecach->save($ach);
      		$this->success("技术成果修改成功！",U('tecach'));
      	}

      } 
      
      public function del(){
      	$map['id']	=   I('id');
      	$this->delete('TbTeach',$map,array('success' => '技术成果删除成功！', 'error' => '技术成果删除失败！'));
      }
      
      //活动中心（所有展会）
      public function allzhanhui(){
      	$zhanhui = M(tb_exhinf);
      
      	
      	$sql = 'SELECT * FROM duijie_tb_exhinf;';
      	$list = $zhanhui-> query($sql);
      	$this -> assign('_list',$list);
      
      	$this->meta_title = '展会列表';
      	$this->display();
      }
      
      //我要报名
      public function enter($id=''){
      
      	if(IS_POST){
      		$exh = M(tb_exhlab);
      		$update[zID] = $_POST['zid'];
      		//	dump($_POST['zid']);
      		$update[jID] = $_POST['jID'];
      		//	dump($_POST['jID']);
      		$rst = $exh ->add($update);
      		if($rst==''){
      			$this->success('报名成功');
	      			 }else {
	      		$this->error('报名失败');
      		}
      
      	}else{
      		$tec = M(tb_tecach);
      		
      		$where['cID'] = UID;
      		$data = $tec -> where($where)->select();

      		$data[zid]= $id;
      		 //	dump($data);
      		$this->assign('data',$data);
      		$this->display();
      	}
      
      	//	$this->display();
      }
      
      //个人中心 我参加的展会
      public function myzhanhui(){
      	 
      	$zhanhui = M(tb_exhinf);
      	$map[cid] = UID;
      	 
      	$list = $zhanhui -> join('duijie_tb_exhlab on duijie_tb_exhlab.zID = duijie_tb_exhinf.id')
      	-> join('duijie_tb_tecach on duijie_tb_tecach.id= duijie_tb_exhlab.jID')
      	-> field('duijie_tb_exhinf.*')
      	-> where($map)
      	-> select();
      	 
      	//	dump($list);
      	$this -> assign('_list',$list);
      	 
      	$this->meta_title = '展会列表';
      	$this->display();
      }
      
      //参展需求
      public function zxq($id=''){
      	$demand = M(tb_demand);
      	$sql='SELECT * FROM duijie_tb_demand,duijie_tb_exhdem,duijie_tb_tecdem
              WHERE duijie_tb_exhdem.xID = duijie_tb_demand.id and duijie_tb_tecdem.id=duijie_tb_demand.rid';
      	$map[zID] = $id;
      	$list = $demand -> where($map)-> query($sql);
        
      	foreach ($list as $key => $value) {
      		$str=$list[$key]['jsbq'];
      		$mapp['id']=array('in',$str);
      		$list[$key]['tids']=M("TbLable")->where($mapp)->select();
      	}
      	
      	foreach ($list as $key => $value) {
      		$str1=$list[$key]['fplace'];
      		$str2=$list[$key]['place'];
      		$map1['id']=$str1;
      		$map2['id']=$str2;
      		$list[$key]['tids1']=M("District")->where($map1)->select();
      		$list[$key]['tids2']=M("District")->where($map2)->select();
      	}
      	
      	
      	$this -> assign('_list',$list);
        //dump($list);
      	$this->meta_title = '参展项目';
      	$this->display();
      }
      
      //展会详情
      public function zdetail($id=''){
      
      	$zhanhui = M(tb_exhinf);
      	$data = $zhanhui -> find($id);
      	$this -> assign('data',$data);
      
      	$this->meta_title = '展会详情';
      	$this->display();
      }
      
      public function myzdetail($id=''){
      
      	$zhanhui = M(tb_exhinf);
      	$data = $zhanhui -> find($id);
      	$this -> assign('data',$data);
      
      	$this->meta_title = '展会详情';
      	$this->display();
      }
      
      
      //需求详情
      public function xqdetail($id=''){
      	$did=I('id');
      	//			dump($did);
      	$demand	=	M('tb_demand');
      
      	$where['duijie_tb_demand.id']=$did;
      	$data=$demand->join('duijie_tb_tecdem on duijie_tb_tecdem.id=duijie_tb_demand.rid')
      	->join('duijie_tb_teccoo on duijie_tb_teccoo.cid=duijie_tb_demand.cooperation')
      	->join('duijie_tb_teccsd on duijie_tb_teccsd.sid=duijie_tb_demand.csd')
      	// 				->field('duijie_tb_teccoo.*,duijie_tb_demand.id idsss')
      	->where($where)
      	->select();
      	//	dump($data);
      	int_to_string($data,array('xdstatus'=>array(-1=>'未对接',0=>'对接中',1=>'对接已结束')));              //数字转为字符串
      	//	dump($data);
      	foreach ($data as $key => $value) {
      		$str=$data[$key]['fplace'];
      		$str2=$data[$key]['place'];
      		$map['id']=$str;
      		$map2['id']=$str2;
      		$data[$key]['tids']=M("District")->where($map)->select();
      		$data[$key]['tids2']=M("District")->where($map2)->select();
      	}
        
      	
      	
      	foreach ($data as $key => $value) {
      		$str3=$data[$key]['ffield'];
      		$str4=$data[$key]['field'];
      		$map3['ID']=$str3;
      		$map4['ID']=$str4;
      		$data[$key]['tids3']=M("TbTeclab")->where($map3)->select();
      		$data[$key]['tids4']=M("TbTeclab")->where($map4)->select();
      	}
      	//  dump($data);
      	// $data->find($id);
      	$this->assign('data', $data);
      	// 			    dump($data);
      
      	$this->display();
      }
      
      
      public function config(){
      	 
      	$aUid = I('get.uid', is_login(), 'intval');
      	$aTab = I('get.tab', '', 'op_t');
      	$aNickname = I('post.nickname', '', 'op_t');
      	$aSex = I('post.sex', 0, 'intval');
      	$aEmail = I('post.email', '', 'op_t');
      	$aSignature = I('post.signature', '', 'op_t');
      	$aCommunity = I('post.community', 0, 'intval');
      	$aDistrict = I('post.district', 0, 'intval');
      	$aCity = I('post.city', 0, 'intval');
      	$aProvince = I('post.province', 0, 'intval');
      
      
      	if (IS_POST) {
      		$this->checkNickname($aNickname);
      		$this->checkSex($aSex);
      		$this->checkSignature($aSignature);
      
      
      		$user['pos_province'] = $aProvince;
      		$user['pos_city'] = $aCity;
      		$user['pos_district'] = $aDistrict;
      		$user['pos_community'] = $aCommunity;
      
      		$user['nickname'] = $aNickname;
      		$user['sex'] = $aSex;
      		$user['signature'] = $aSignature;
      		$user['jsbq'] = $aJsbq;
      		$user['uid'] = get_uid();
      
      		$rs_member = D('Member')->save($user);
      
      		$ucuser['id'] = get_uid();
      		$rs_ucmember = UCenterMember()->save($ucuser);
      		clean_query_user_cache(get_uid(), array('nickname', 'sex', 'signature', 'email', 'pos_province', 'pos_city', 'pos_district', 'pos_community'));
      		 
      
      
      
      		//TODO tox 清空缓存
      
      		S('weibo_at_who_users',null);
      
      		if ($rs_member || $rs_ucmember) {
      			$this->success(L('_SUCCESS_SETTINGS_').L('_PERIOD_'),U('admin/public/login'));
      
      		} else {
      			$this->success(L('_DATA_UNMODIFIED_').L('_PERIOD_'));
      		}
      
      	} else {
      		//调用API获取基本信息
      		//TODO tox 获取省市区数据
      		$user = query_user(array('nickname', 'signature', 'email', 'mobile', 'avatar128', 'rank_link', 'sex', 'pos_province', 'pos_city', 'pos_district', 'pos_community'), $aUid);
      		//显示页面
      		$this->assign('user', $user);
      
      		$this->accountInfo();
      
      		$result = M('tb_teclab')->where(array('upid'=> 0))->field('id,name')->select();
      		$this->assign('cate',$result);
            
      	//dump($result);
      		
      		$jsbq=M('tb_lable')->select();
      		$this->assign('jsbq',$jsbq);
      		//dump($jsbq);
      		$this->assign('tab', $aTab);
      		$this->getExpandInfo();
      		$this->_setTab('info');
      		$this->display();
      	}
      
      }
      private function accountInfo()
      {
      	$info = UCenterMember()->field('id,username,email,mobile,type')->find(is_login());
      	$this->assign('accountInfo', $info);
      }
       
      public function getExpandInfo($uid = null)
      {
      	$profile_group_list = $this->_profile_group_list($uid);
      	if ($profile_group_list) {
      		$info_list = $this->_info_list($profile_group_list[0]['id'], $uid);
      		$this->assign('info_list', $info_list);
      		$this->assign('profile_group_id', $profile_group_list[0]['id']);
      		//dump($info_list);exit;
      	}
      	foreach ($profile_group_list as &$v) {
      		$v['fields'] = $this->_getExpandInfo($v['id']);
      	}
      
      	$this->assign('profile_group_list', $profile_group_list);
      }
      
      public function _profile_group_list($uid = null)
      {
      	$profile_group_list = array();
      	$fields_list = $this->getRoleFieldIds($uid);
      	if ($fields_list) {
      		$fields_group_ids = D('FieldSetting')->where(array('id' => array('in', $fields_list), 'status' => '1'))->field('profile_group_id')->select();
      		if ($fields_group_ids) {
      			$fields_group_ids = array_unique(array_column($fields_group_ids, 'profile_group_id'));
      			$map['id'] = array('in', $fields_group_ids);
      
      			if (isset($uid) && $uid != is_login()) {
      				$map['visiable'] = 1;
      			}
      			$map['status'] = 1;
      			$profile_group_list = D('field_group')->where($map)->order('sort asc')->select();
      		}
      	}
      	return $profile_group_list;
      }
      
      private function getRoleFieldIds($uid = null)
      {
      	$role_id = get_role_id($uid);
      	$fields_list = S('Role_Expend_Info_' . $role_id);
      	if (!$fields_list) {
      		$map_role_config = getRoleConfigMap('expend_field', $role_id);
      		$fields_list = D('RoleConfig')->where($map_role_config)->getField('value');
      		if ($fields_list) {
      			$fields_list = explode(',', $fields_list);
      			S('Role_Expend_Info_' . $role_id, $fields_list, 600);
      		}
      	}
      	return $fields_list;
      }
      
      private function _setTab($name)
      {
      	$this->assign('tab', $name);
      }
      
}